package com.yl.chainStore.util


import com.yl.chainStore.entity.pay.SysCompanyPayItemType
import com.yl.chainStore.entity.sick.CustomerSubscribeItem
import com.yl.chainStore.entity.system.CompanyDictionaryDetail
import com.yl.chainStore.entity.user.CompanyUserMedicalHistoryModelType
import com.yl.chainStore.pojo.vo.WebSelectVo
import com.yl.chainStore.pojo.vo.sick.CustomerSubscribeItemResultVo
import com.yl.common.pojo.vo.TreeSelect
import java.util.function.Consumer



fun payItemTypeListToTree(list: MutableList<SysCompanyPayItemType>, pId: Long?): List<SysCompanyPayItemType> {
    val tree: MutableList<SysCompanyPayItemType> = ArrayList()
    val it: MutableIterator<SysCompanyPayItemType> = list.iterator()
    while (it.hasNext()) {
        val m: SysCompanyPayItemType = it.next()
        if (m.superId == pId) {
            tree.add(m)
            // 已添加的元素删除掉
            it.remove()
        }
    }
    // 寻找子元素
    tree.forEach(Consumer { n: SysCompanyPayItemType ->
        n.children = payItemTypeListToTree(list, n.id)

    })
    return tree
}

fun payItemTypeListToTreeSelect(list: MutableList<SysCompanyPayItemType>, pId: Long?): List<TreeSelect> {
    val tree: MutableList<TreeSelect> = ArrayList()
    val it: MutableIterator<SysCompanyPayItemType> = list.iterator()
    while (it.hasNext()) {
        val m: SysCompanyPayItemType = it.next()
        if (m.superId == pId) {
            tree.add(TreeSelect(id=m.id,name = m.name))
            // 已添加的元素删除掉
            it.remove()
        }
    }
    // 寻找子元素
    tree.forEach(Consumer { n: TreeSelect ->
        n.children = payItemTypeListToTreeSelect(list, n.id)

    })
    return tree
}

/**
 * 字典联动下拉使用
 */
fun dictionaryListToTree(list:MutableList<CompanyDictionaryDetail>,pId: Long?):List<CompanyDictionaryDetail>{
    val tree: MutableList<CompanyDictionaryDetail> = ArrayList()
    val it: MutableIterator<CompanyDictionaryDetail> = list.iterator()
    while (it.hasNext()) {
        val m: CompanyDictionaryDetail = it.next()
        if (m.parentId == pId) {
            tree.add(m)
            // 已添加的元素删除掉
            it.remove()
        }
    }
    // 寻找子元素
    tree.forEach(Consumer { n: CompanyDictionaryDetail ->
        n.childList = dictionaryListToTree(list, n.id)

    })
    return tree
}

/**
 * 字典联动下拉使用
 */
fun dictionaryListToTreeSelect(list:MutableList<CompanyDictionaryDetail>,pId: Long?):List<TreeSelect>{
    val tree: MutableList<TreeSelect> = ArrayList()
    val it: MutableIterator<CompanyDictionaryDetail> = list.iterator()
    while (it.hasNext()) {
        val m: CompanyDictionaryDetail = it.next()
        if (m.parentId == pId) {
            tree.add(TreeSelect(id=m.id,name = m.itemName))
            // 已添加的元素删除掉
            it.remove()
        }
    }
    // 寻找子元素
    tree.forEach(Consumer { n: TreeSelect ->
        n.children = dictionaryListToTreeSelect(list, n.id)

    })
    return tree
}

fun customerSubscribeItemToTree(itemList:MutableList<CustomerSubscribeItem>):List<CustomerSubscribeItemResultVo>{
    val list = mutableListOf<CustomerSubscribeItemResultVo>()
    val map = mutableMapOf<String,CustomerSubscribeItemResultVo>()
    for(item in itemList){
       var vo = map[item.typeName]
        if(vo == null){
            vo = CustomerSubscribeItemResultVo(title = item.typeName)
            list.add(vo)
            map[item.typeName] = vo
        }
        vo.children.add(WebSelectVo(id=item.id,name = item.itemName))
    }
    return list
}

fun medicalHistoryTypeToTree(list: MutableList<CompanyUserMedicalHistoryModelType>,pId: Long?):MutableList<CompanyUserMedicalHistoryModelType>{
    val tree: MutableList<CompanyUserMedicalHistoryModelType> = ArrayList()
    val it: MutableIterator<CompanyUserMedicalHistoryModelType> = list.iterator()
    while (it.hasNext()) {
        val m: CompanyUserMedicalHistoryModelType = it.next()
        if (m.parentId == pId) {
            tree.add(m)
            // 已添加的元素删除掉
            it.remove()
        }
    }
    // 寻找子元素
    tree.forEach(Consumer { n: CompanyUserMedicalHistoryModelType ->
        n.children = medicalHistoryTypeToTree(list, n.id)

    })
    return tree
}










